package bookstore.servlet;

import bookstore.bound.book.BookReviewForm;
import bookstore.bound.book.BookReviewRespForm;
import bookstore.manager.AuthManager;
import bookstore.manager.BookManager;
import bookstore.model.Book;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/api/market")
public class MarketServlet {
    protected BookManager bookManager;
    protected AuthManager authManager;

    public MarketServlet(BookManager bookManager, AuthManager authManager) {
        System.out.println(String.format("Constructor of %s", this.getClass().getName()));
        this.bookManager = bookManager;
        this.authManager = authManager;
    }

    @GetMapping("/book")
    public ArrayList getBookList() {
        return bookManager.getAllBooks();
    }

    @GetMapping("/book/{isbn}")
    public Map getBookDetail(@PathVariable("isbn") String isbn) {
        return bookManager.getBookDetail(isbn);
    }

    @PostMapping("/book/{isbn}/review")
    public Map AddReview(HttpSession session, @PathVariable("isbn") String isbn, @RequestBody BookReviewForm bookReviewForm) {
        authManager.requireLogin(session);
        return bookManager.AddReview(isbn, session, bookReviewForm);
    }

    @PostMapping("/book/{isbn}/review/{reviewid}")
    public Map AddReviewResp(HttpSession session, @PathVariable("isbn") String isbn, @PathVariable("reviewid") int reviewid, @RequestBody BookReviewRespForm bookReviewRespForm) {
        authManager.requireLogin(session);
        return bookManager.AddReviewResp(isbn, session, reviewid, bookReviewRespForm);
    }
}
